﻿package core.drawing
{
    import __AS3__.vec.*;
    import core.enum.*;
    import core.image.*;
    import flash.events.*;
    import flash.geom.*;

    final public class LassoTool extends SelectionTool
    {
        public var cords:Vector.<Point>;
        public var draws:Vector.<Point>;

        public function LassoTool(param1:Tuyitu)
        {
            super(param1, ToolType.Lasso);
            return;
        }// end function

        override public function MouseDown(event:MouseEvent) : void
        {
            this.UpdateReferences();
            if (selection != null && !event.shiftKey && !event.ctrlKey)
            {
            }
            switch(options.action)
            {
                case Action.FreehandLassoTool:
                {
                    break;
                }
                case Action.PolygonLassoTool:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override public function MouseDrag(event:MouseEvent) : void
        {
            switch(options.action)
            {
                case Action.FreehandLassoTool:
                {
                    po = canvas.GetRealMousePosition();
                    Misc.DrawLine(canvas.coating.bitmapData, pt, po);
                    this.cords.push(canvas.GetAverageMousePosition());
                    pt = po;
                    break;
                }
                case Action.PolygonLassoTool:
                {
                    this.draws[(this.draws.length - 1)] = canvas.GetRealMousePosition();
                    canvas.coating.Clear();
                    Misc.DrawPolygon(canvas.coating.bitmapData, this.draws);
                    canvas.coating.Redraw();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override public function MouseUp(event:MouseEvent) : void
        {
            var _loc_2:Point = null;
            if (options.action == Action.PolygonLassoTool)
            {
                if (this.cords.length > 2)
                {
                    pt = this.cords[0];
                    po = this.cords[(this.cords.length - 1)];
                    _loc_2 = this.cords[this.cords.length - 2];
                    if (_loc_2.x == po.x && _loc_2.y == po.y || po.x < pt.x + 5 && po.x > pt.x - 5 && po.y < pt.y + 5 && po.y > pt.y - 5)
                    {
                        this.SetSelected(event.shiftKey, event.ctrlKey);
                    }
                }
            }
            else
            {
                this.SetSelected(event.shiftKey, event.ctrlKey);
            }
            return;
        }// end function

        private function SetSelected(Reset:Boolean, Reset:Boolean) : void
        {
            ed.cursor.freeze = false;
            canvas.back.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove, false, 0, true);
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.MouseUp, false);
            if (this.cords.length < 2 && !Reset && !Reset)
            {
                history.ChangePrepareType(Action.Deselect);
                canvas.RemoveSelection();
            }
            else
            {
                if (selection == null)
                {
                    canvas.AddSelection();
                    selection = canvas.selection;
                }
                else if (!Reset && !Reset)
                {
                    selection.Reset();
                }
                canvas.coating.Clear();
                selection.EditPolygon(this.cords, options.feather, options.antialias, Boolean(!Reset && Reset));
                selection.CreateOutline();
                canvas.coating.Redraw();
                ed.navigator.SetSelectionSize(selection.width, selection.height);
                if (selection.width == 0 || selection.height == 0)
                {
                    canvas.RemoveSelection();
                }
            }
            history.Commit();
            this.cords = null;
            this.draws = null;
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

    }
}
